package dao

import (
	"goblog/models"
	"github.com/astaxie/beego/orm"
	"fmt"
)

type LinkDao struct {
	Dao orm.Ormer
}

type LinkTerm struct {
	models.Term
	Name string
}

func (this *LinkDao) Find(term *LinkTerm) {
	if this.Dao == nil {
		this.Dao = orm.NewOrm()
	}
	var args []interface{}
	var links []models.Link
	sql := "select `id`, `name`, `url` from link where 1=1 "
	if len(term.Name) > 0 {
		sql += "and name like ?"
		args = append(args, "%"+term.Name+"%")
	}
	id, err := Paging(sql, args, &term.Term, this.Dao).QueryRows(&links)
	if err != nil {
		panic(err)
		fmt.Println(id)
	}
	term.Data = links
}

func (this *LinkDao) Del(id int) {
	if this.Dao == nil {
		this.Dao = orm.NewOrm()
	}
	_, err := this.Dao.Raw("delete from link where id=?", id).Exec();
	if err != nil {
		panic(err)
	}
}
